﻿<html>
<head>
	<title>计数排序</title>
</head>
<body>
	<script language="JavaScript"> 
	function countingSort(array) {
		var len = array.length
		B = [],
		C = [],
		min = max = array[0];
		for (var i = 0; i < len; i++) {
			min = min <= array[i] ? min : array[i];
			max = max >= array[i] ? max : array[i];
			C[array[i]] = C[array[i]] ? C[array[i]] + 1 : 1;
		}
		for (var j = min; j < max; j++) {
			C[j + 1] = (C[j + 1] || 0) + (C[j] || 0);
		}
		for (var k = len - 1; k >= 0; k--) {
			B[C[array[k]] - 1] = array[k];
			C[array[k]]--;
		}
		return B.toString();
	}
	var arr = [59,34,25,67,15,87,10,99,3,45];
	console.log(countingSort(arr)); 
</script>
</body>
</html>